package com.central.apps.controller;


import com.central.app.common.util.DateUtils;
import com.central.app.service.HandlerService;
import com.central.common.web.Result;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 库存面板接口
 */
@Log4j2
@RestController
@RequestMapping("/InventoryPanel")
public class InventoryPanelController {

    @Autowired
    private HandlerService handlerService;

    //库存金额占比  从库存台账成本表取，根据物料大类进行分类取金额
    @GetMapping("/inventoryAmount")
    public Result inventoryAmount(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> bySql = new ArrayList<>();
        try {
            bySql = handlerService.findBySql("SELECT b.maxdesc as `name` ,sum(a.avgcost) as `value` from gq_invcost a left join gq_item b on a.itemnum=b.itemnum where b.maxdesc is not null GROUP BY b.maxdesc ");
            return  Result.succeed(bySql,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(bySql,"查询成功");
    }

    //课题库存金额  从库存台账成本表取，根据课题号进行分类取金额
    @GetMapping("/projectNumberAmount")
    public Result projectNumberAmount(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> bySql = new ArrayList<>();
        try {
            bySql = handlerService.findBySql("SELECT a.subjectno as `name` ,sum(a.avgcost) as `value` from gq_invcost a where a.subjectno is not null GROUP BY a.subjectno ");
            return  Result.succeed(bySql,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(bySql,"查询成功");
    }

    //库房金额占比  从库存台账成本表取，根据主表的库房进行分类取金额
    @GetMapping("/storeAmount")
    public Result storeAmount(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> bySql = new ArrayList<>();
        try {
            bySql = handlerService.findBySql("SELECT a.storenum,any_value ( b.storedesc ) as `name` ,sum( a.avgcost ) as `value` FROM gq_invcost a LEFT  JOIN gq_store b on a.storenum=b.storenum where a.storenum IS NOT NULL and b.storedesc is not null GROUP BY a.storenum");
            return  Result.succeed(bySql,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(bySql,"查询成功");
    }

    //年库存金额统计 根据库存月结所有库房每年12月月结的结存金额和
    @GetMapping("/yearInventoryAmount")
    public Result yearInventoryAmount(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> bySql = new ArrayList<>();
        try {
            bySql = handlerService.findBySql("SELECT `year`,sum(totalcost) as totalcost from gq_query where `month`='12' GROUP BY `year`");
            return  Result.succeed(bySql,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(bySql,"查询成功");
    }

    //月库存金额统计  根据库存月结所有库房每月月结结存金额和（只限当年的）
    @GetMapping("/monthInventoryAmount")
    public Result monthInventoryAmount(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> bySql = new ArrayList<>();
        try {
            int year = DateUtils.getCurrentYear();
            bySql = handlerService.findBySql("SELECT `month`,sum(totalcost) as totalcost from gq_query where `year` = " + year + " GROUP BY `month`");
            return  Result.succeed(bySql,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(bySql,"查询成功");
    }

    //月出入库情况   根据出入库记录统计每月出入库金额
    @GetMapping("/monthIntoOut")
    public Result monthIntoOut(@RequestParam Map<String, Object> params) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            int year = DateUtils.getCurrentYear();
            //存储一共的月份。
            Set<String> monthAll = new HashSet<>();
            //入库记录
            Map<String, Object> mpnthIntoMap = new HashMap<>();
            List<Map<String, Object>> mpnthInto = handlerService.findBySql("SELECT MONTH( createtime) as `month` , sum( linecost ) as linecost FROM gq_matusetrans  WHERE year( createtime ) = "+year+" and  type in ('接收','退回','报废','调入') GROUP BY MONTH( createtime)");
            mpnthInto.stream().forEach(stringObjectMap -> {
                String month = MapUtils.getString(stringObjectMap, "month");
                String linecost = MapUtils.getString(stringObjectMap, "linecost");
                monthAll.add(month);
                mpnthIntoMap.put(month,linecost);
            });
            //出库记录
            Map<String, Object> mpnthOutMap = new HashMap<>();
            List<Map<String, Object>> mpnthOut = handlerService.findBySql("SELECT MONTH( createtime) as `month` , sum( linecost ) as linecost FROM gq_matusetrans  WHERE year( createtime ) = "+year+" and  type in ('报废出库','领用出库','销售出库','返厂维修','调出') GROUP BY MONTH( createtime)");
            mpnthOut.stream().forEach(stringObjectMap -> {
                String month = MapUtils.getString(stringObjectMap, "month");
                String linecost = MapUtils.getString(stringObjectMap, "linecost");
                monthAll.add(month);
                mpnthOutMap.put(month,linecost);
            });

            //整理数据
            List<String> y1List = new ArrayList<>();//入库
            List<String> y2List = new ArrayList<>();//出库

            resultMap.put("x",monthAll);//x轴
            for (String s : monthAll) {
                //入库
                String string = MapUtils.getString(mpnthIntoMap, s);
                if(StringUtils.isNotBlank(string)){
                    y1List.add(string);
                }else{
                    y1List.add("0");
                }

                //出库
                String string1 = MapUtils.getString(mpnthOutMap, s);
                if(StringUtils.isNotBlank(string1)){
                    y2List.add(string1);
                }else{
                    y2List.add("0");
                }
            }
            resultMap.put("y1",y1List);
            resultMap.put("y2",y2List);

            return  Result.succeed(resultMap,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  Result.succeed(resultMap,"查询成功");
    }

}

